/* Hawk - A Component Based Test Framework
 *
 * Copyright (C) 2010 http://code.google.com/p/hawk-framework/
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License. 
 */
package org.hawk.applications.finder.swing.communication;

import java.io.IOException;

import javax.bluetooth.BluetoothStateException;
import javax.bluetooth.DeviceClass;
import javax.bluetooth.RemoteDevice;
import javax.bluetooth.ServiceRecord;

import org.hawk.applications.finder.swing.gui.TabProfile;

import net.java.dev.marge.entity.ClientDevice;
import net.java.dev.marge.entity.config.ClientConfiguration;
import net.java.dev.marge.factory.CommunicationFactory;
import net.java.dev.marge.factory.RFCOMMCommunicationFactory;
import net.java.dev.marge.inquiry.DeviceDiscoverer;
import net.java.dev.marge.inquiry.InquiryListener;
import net.java.dev.marge.inquiry.ServiceDiscoverer;
import net.java.dev.marge.inquiry.ServiceSearchListener;

/**
 * 
 * @author Rodrigo Prestes Machado
 */
public class ThreadClient extends Thread {

	public void run() {
		
		try {
			System.out.println("Starting inquiry for device ...");
			
			// Starting GIAC inquiry for devices. You can do LIAC inquiries changing the method
			DeviceDiscoverer.getInstance().startInquiryGIAC(new InquiryListenerImpl());
		}
		catch (BluetoothStateException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 
	 * @author Rodrigo Prestes Machado
	 */
	private class InquiryListenerImpl implements InquiryListener{

		@Override
		public void deviceDiscovered(RemoteDevice device, DeviceClass arg1) {
			
			// Debug
			System.out.println("Device Found: " + device);
			System.out.println("Looking for services ...");
			
			try {
				// Starting search for services in a found device
				ServiceDiscoverer.getInstance().startSearch(device, new ServiceSearchListenerImpl());
			} 
			catch (BluetoothStateException e) {
				e.printStackTrace();
			}
		}

		@Override
		public void inquiryCompleted(RemoteDevice[] arg0) {
			System.out.println("Inquiry Completed");
			
		}

		@Override
		public void inquiryError() {
			System.out.println("Inquiry Error");
		}
	}
	
	/**
	 * 
	 * @author Rodrigo Prestes Machado
	 */
	public class ServiceSearchListenerImpl implements ServiceSearchListener {

		@Override
		public void deviceNotReachable() {
			System.out.println("Device not Reachable");
		}

		@Override
		public void serviceSearchCompleted(RemoteDevice device, ServiceRecord[] services) {
			
			// Gets the first service from connected device
			ServiceRecord service = services[0];
			System.out.println("Service found from: " + service.getHostDevice() + " device");
			
			try {
				//Creates a client configuration using the specific ServiceRecord found 
				//by ServiceSearcher
				ClientConfiguration clientConfig = new ClientConfiguration(service, new CommunicationListenerImpl());

				//Create a protocol factory to get the client
				CommunicationFactory factory = new RFCOMMCommunicationFactory();

				//Gets the client device instance
				ClientDevice clientDevice = factory.connectToServer(clientConfig);
				
				// Starting listening for incoming messages
				clientDevice.startListening();

				// Device sending the profile
				clientDevice.send(TabProfile.getProfile());

				//TODO fix the close
				// Closing connection
				//clientDevice.close();
			} 
			catch (IOException e) {
				e.printStackTrace();
			}			
		}

		@Override
		public void serviceSearchError() {
			System.out.println("Service Search Error");	
		}
	}
}